package client;

import java.io.Serializable;

import javax.swing.JOptionPane;

/**
 * 
 * Register members and receive members accounts
 *
 */
public class MemberAccount implements Serializable{
	
	/**
	 * credit card
	 */
	public CreditCard cc;
	/**
	 * members id number
	 */
	private int usrID;
	
	/**
	 * members secondary id number
	 */
	public String idnumber;
	/**
	 * member address
	 */
	public String address;
	/**
	 * member phone number
	 */
	public String phone;
	/**
	 * storage details and send to echo server and data base
	 */
	public String snl;
	/**
	 * storage details and send to echo server and data base
	 */
	public String sn[]=new String[2];
	/**
	 * storage details and send to echo server and data base
	 */
	public String snF[]=new String[3];
	/**
	 * saves details from data base for tables in GUI
	 */
	public static Object[][] data = null;
	
	/**
	 *  Initialize member account WITH credit card
	 * @param creditCardNumber credit card number
	 * @param expireMonth credit card month expire date
	 * @param expireYear credit card year expire date
	 * @param address member address
	 * @param phone member phone number
	 * @param idNumber member secondary id number
	 * @param uid member id number
	 */
	public MemberAccount(String creditCardNumber, String expireMonth, String expireYear,String address,String phone,String idNumber,int uid)
	{
		cc = new CreditCard(creditCardNumber, expireMonth, expireYear);
		this.address=address;
		this.phone=phone;
		this.idnumber=idNumber;
		this.usrID=uid;
	}
	
	/**
	 * Initialize member account WITHOUT credit card
	 * @param uid member id number
	 * @param phone member phone number
	 * @param add member address
	 * @param idNum member secondary id number
	 */
	public MemberAccount(int uid,String phone,String add,String idNum)
	{
		this.usrID = uid;
		this.address=add;
		this.idnumber=idNum;
		this.phone=phone;
	}
	/**
	 * Initialize member 
	 * @param uid = member id
	 */
	public MemberAccount(Object uid) {
		usrID = new Integer(uid.toString());
	}
	
	public MemberAccount()
	{  }

	/**
	 * Gets credit card
	 * @return credit card details
	 */
	public CreditCard getCreditCard()
	{
		return cc;
	}
	
	/**
	 * Gets member ID number
	 * @return member id number
	 */
	public int getUserID()
	{
		return usrID;
	}
	
	/**
	 * Validates ID number
	 * @return 1 if legal,else 0
	 */
	public int checkID()
	{
		if(idnumber.length()==0)
		{
			JOptionPane.showMessageDialog(null, "ID is required");
			return 0;
		}
		if(idnumber.length()!=9)
		{
			JOptionPane.showMessageDialog(null, "ID has to be 9 digits");
			return 0;
		}
		for(int i=0;i<idnumber.length();i++)
			if(!Character.isDigit(idnumber.charAt(i)))
			{
				JOptionPane.showMessageDialog(null, "Digits only");
				return 0;
			}
		return 1;
	}
	
	/**
	 * User submit request for an account
	 * @return 1 if operation succeeded,else 0
	 */
	public int submit()
	{
		ObjHandle.sendToServer(this);
		
		return (Integer)Main.clie.getResult();
	}
	/**
	 * 
	 * @param c = 1 or 0
	 * @return if c=1 user details if c=0 user privilege
	 */
	public  Object[][] OK(int c)
	{
		
		if (c==1)
			sn[0] = "get user details";
		else
			sn[0] = "get user priv";
		sn[1] = Integer.toString(usrID);
		
		ObjHandle.sendToServer(sn);
		
		if(c==1)
		{
			data=new Object[((Object[][]) Main.clie.getResult()).length][3];
			for(int i=0 ; i<((Object[][]) Main.clie.getResult()).length ; i++)
				data[i][2] = new Boolean(null);
		}
		else
			data=new Object[((Object[][]) Main.clie.getResult()).length][3];
		data= (Object[][]) Main.clie.getResult();
		return data;
	}
	/**
	 * freeze or recover user account
	 * @param data if 1 freeze account if 0 recover account
	 */
	public void changeFrozen(String data) {
		
		snF[0] = "change frozen details";
		snF[1] = Integer.toString(usrID);
		snF[2] = data;
		
		ObjHandle.sendToServer(snF);
	}
	/**
	 * 
	 * @return all frozen accounts
	 */
	public Object [][] getFrozenAccounts()
	{
		snl = "get frozen accounts";
		
		ObjHandle.sendToServer(snl);
		
		data=new Object[((Object[][]) Main.clie.getResult()).length][3];
		data= (Object[][]) Main.clie.getResult();
		return data;
	}
	/**
	 * 
	 * @param priv desirable privilege to set 
	 */
	public void Set(String priv)
	{
		snF[0] = "change user priv";
		snF[1] = Integer.toString(usrID);
		snF[2] = priv;
		
		ObjHandle.sendToServer(snF);	
	}
	/**
	 * 
	 * @param id user id
	 * @param balance money to add or reduce
	 * @param cid complaint id
	 */
	public static void SetBalance(String id, String balance, String cid)
	{
		String []snB = new String[4];
		snB[0] = "set balance";
		snB[1] = id;
		snB[2] = balance;
		snB[3] = cid;
		
		ObjHandle.sendToServer(snB);
	}
}
